from djoser.serializers import UserCreateSerializer,UserSerializer
from rest_framework import serializers
from rest_framework.validators import UniqueValidator
from django.contrib.auth.models import User
from account.models import Profile

class CustomUniqueValidator(UniqueValidator):
    
    def __call__(self, value, serializer_field):
        
        self.message = "邮箱已经存在"
        
        return super().__call__(value, serializer_field)



class CustomUserCreateSerializer(UserCreateSerializer):
    email = serializers.EmailField(
        validators = [CustomUniqueValidator(queryset=User.objects.all())]
    )
    
    class Meta:
        model = User
        fields = ('id',"username","email","password")
        
    # 重写create方法，写入profile表
    def create(self, validated_data):
        user = UserCreateSerializer.create(self,validated_data)
        
        # 写入到profile表
        profile = Profile(user = user)
        profile.save()
        return user
    
class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = "__all__"

class CustomUserSerializer(UserSerializer):
    
    profile = ProfileSerializer(read_only = True)
    
    class Meta(UserSerializer.Meta):
        fields = [*UserSerializer.Meta.fields,'profile']
        